//
//  FCBaseViewController.m
//  OilWear
//
//  Created by jjkj on 17/7/10.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "FCBaseViewController.h"
#import "UIColor+Hex.h"
#import "FCSwitchView.h"
#import "FCVehicleStatusController.h"
#import "YZHomePageViewCtl.h"
#import "YZInputOrOutputGasolineViewController.h"
#import "ChooseDateView.h"
#import "CLHomeCarViewController.h"

@interface FCBaseViewController ()

@property (nonatomic, assign) RolePower rolePower;//权限
@property (nonatomic, assign) HomePageType homePageType;//主页类型
@property (nonatomic, strong) YZHomePageViewCtl *yzViewCtl;
@property (nonatomic, strong) CLHomeCarViewController *clViewCtl;

@end

@implementation FCBaseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
  
    self.rolePower = [CommClass getRolePowerWith:[UserLoginModel getPower]];
    [self preferredStatusBarStyle];

    [self setUpSwitchView];
    [self showOrHideSwitchView:self.rolePower];
 

    // Do any additional setup after loading the view.
}


- (void)DateSelectedButtonClick:(UIButton *)sender{

    __weak typeof(self) weakself = self;
    
    if (!self.dateView) {
        
        self.dateView = [[ChooseDateView alloc]initWithFrame:self.view.bounds];
        self.dateView.backgroundColor = [UIColor whiteColor];
        [self.dateView setHidden:NO];
        
        self.dateView.selectDateCompletionHandle = ^(NSString *beginTime, NSString *endTime){
            
            NSLog(@"%@ - %@",beginTime, endTime);
            if (_homePageType == HomePageTypeYZ) {
                [weakself.yzViewCtl getYZHomeInfoWithStartTime:beginTime endTime:endTime];
                [weakself.yzViewCtl setRequestStartTime:beginTime endTime:endTime];
            }else {
                [weakself.clViewCtl getHomeCarInfoListWith:beginTime andEndTime:endTime];
                weakself.clViewCtl.startTime = endTime;
                weakself.clViewCtl.endTime = endTime;
            }
        };
        
        [self.view addSubview:self.dateView];
    }
    else{
      
        [self.dateView setHidden:NO];
    }
}

//根据用户设置选项卡显示还是隐藏
-(void)showOrHideSwitchView:(RolePower)power {
    
    if (power==RolePowerCLLeader || power==RolePowerYZLeader) {
        
        //修改switchview高度，显示隐藏
        [self.switchView changeView_Height:0];
        self.switchView.hidden = YES;
        if (power == RolePowerCLLeader) {
            //默认选中状态
            [self changePageWithTag:101];
        }else{
            [self changePageWithTag:100];
        }
    }else {
        //修改switchview高度，显示隐藏
        [self.switchView changeView_Height:50];
        self.switchView.hidden = NO;
    
        [self changePageWithTag:100];
    }
    
}
//设置选项卡
- (void)setUpSwitchView {
    
    self.switchView = [[NSBundle mainBundle] loadNibNamed:@"FCSwitchView" owner:self options:nil].firstObject;
    [self.view addSubview:self.switchView];
    [self.switchView setUpSwitchViewTitleWith:@[@"油耗业务",@"车辆业务"]];
    [self.switchView setUpSwitchButtonAppreace:0];
    __weak typeof(self) weakSelf = self;
    
    self.switchView.switchActionBlock = ^(NSInteger tag){
        
        [weakSelf changePageWithTag:tag];
    };
}
//切换选项卡事件处理
- (void)changePageWithTag:(NSInteger)tag {
    
    if (100 == tag) {
        self.homePageType = HomePageTypeYZ;
        //如果页面已经存在重复点击无效
        if (_yzViewCtl) return;
        
        //如果车辆页面存在，删掉
        if (_clViewCtl) {
            [_clViewCtl.view removeFromSuperview];
            [_clViewCtl removeFromParentViewController];
            _clViewCtl = nil;
        }
        [self addChildViewController:self.yzViewCtl];
        [self.view addSubview:_yzViewCtl.view];
        _yzViewCtl.view.frame = CGRectMake(0, CGRectGetMaxY(self.switchView.frame), kScreenWidth, kScreenHeight-CGRectGetMaxY(self.switchView.frame));
        
    }else {
        self.homePageType = HomePageTypeCL;
        //如果页面已经存在重复点击无效
        if (_clViewCtl) return;
        
        //如果油站页面存在，删掉
        if (_yzViewCtl) {
            [_yzViewCtl.view removeFromSuperview];
            [_yzViewCtl removeFromParentViewController];
            _yzViewCtl = nil;
        }
        [self addChildViewController:self.clViewCtl];
        [self.view addSubview:_clViewCtl.view];
        _clViewCtl.view.frame = CGRectMake(0, CGRectGetMaxY(self.switchView.frame), kScreenWidth, kScreenHeight-CGRectGetMaxY(self.switchView.frame));
    }
}

- (YZHomePageViewCtl *)yzViewCtl {
    
    if (!_yzViewCtl) {
        _yzViewCtl = [[YZHomePageViewCtl alloc]initWithNibName:@"YZHomePageViewCtl" bundle:nil];
    }
    return _yzViewCtl;
}
- (CLHomeCarViewController *)clViewCtl {
    
    if (!_clViewCtl) {
        _clViewCtl = [[CLHomeCarViewController alloc] init];
    }
    return _clViewCtl;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

@end
